/**
 * 数据同步相关类型定义
 */

// 同步类型枚举
export enum SyncType {
  FULL = 'full',
  INCREMENTAL = 'incremental'
}

// 目标类型枚举
export enum TargetType {
  CSV = 'csv',
  DORIS = 'doris',
  KAFKA = 'kafka'
}

// 调度类型枚举
export enum ScheduleType {
  MANUAL = 'manual',
  CRON = 'cron'
}

// 执行状态枚举
export enum ExecutionStatus {
  PENDING = 'pending',
  RUNNING = 'running',
  SUCCESS = 'success',
  FAILED = 'failed',
  PAUSED = 'paused',
  CANCELLED = 'cancelled'
}

// 执行类型枚举
export enum ExecutionType {
  MANUAL = 'manual',
  SCHEDULED = 'scheduled'
}

// 增量字段类型枚举
export enum IncrementalFieldType {
  TIMESTAMP = 'timestamp',
  ID = 'id'
}

// 日志级别枚举
export enum LogLevel {
  DEBUG = 'DEBUG',
  INFO = 'INFO',
  WARNING = 'WARNING',
  ERROR = 'ERROR'
}

// 类型标签映射
export const SyncTypeLabels = {
  [SyncType.FULL]: '全量同步',
  [SyncType.INCREMENTAL]: '增量同步'
};

export const TargetTypeLabels = {
  [TargetType.CSV]: 'CSV文件',
  [TargetType.DORIS]: 'Doris数据库',
  [TargetType.KAFKA]: 'Kafka消息队列'
};

export const ScheduleTypeLabels = {
  [ScheduleType.MANUAL]: '手动执行',
  [ScheduleType.CRON]: '定时调度'
};

export const ExecutionStatusLabels = {
  [ExecutionStatus.PENDING]: '等待中',
  [ExecutionStatus.RUNNING]: '运行中',
  [ExecutionStatus.SUCCESS]: '成功',
  [ExecutionStatus.FAILED]: '失败',
  [ExecutionStatus.PAUSED]: '已暂停',
  [ExecutionStatus.CANCELLED]: '已取消'
};

export const ExecutionTypeLabels = {
  [ExecutionType.MANUAL]: '手动执行',
  [ExecutionType.SCHEDULED]: '定时执行'
};

export const IncrementalFieldTypeLabels = {
  [IncrementalFieldType.TIMESTAMP]: '时间戳',
  [IncrementalFieldType.ID]: '自增ID'
};

export const LogLevelLabels = {
  [LogLevel.DEBUG]: '调试',
  [LogLevel.INFO]: '信息',
  [LogLevel.WARNING]: '警告',
  [LogLevel.ERROR]: '错误'
};

// 字段映射接口
export interface SyncFieldMapping {
  id?: number;
  task_id?: number;
  source_field: string;
  target_field: string;
  field_type?: string;
  transform_expression?: string;
  is_key_field: boolean;
  sort_order: number;
  created_at?: string;
  updated_at?: string;
}

// 同步任务接口
export interface SyncTask {
  id: number;
  name: string;
  display_name: string;
  description?: string;
  
  // 数据源配置
  source_datasource_id: number;
  source_table_name: string;
  source_query?: string;
  
  // 目标配置
  target_type: TargetType;
  target_datasource_id?: number;
  target_table_name?: string;
  target_path?: string;

  // CSV配置
  csv_delimiter?: string;
  csv_quote_char?: string;
  csv_escape_char?: string;
  csv_line_terminator?: string;
  csv_encoding?: string;
  csv_include_header?: boolean;
  csv_null_value?: string;
  csv_date_format?: string;
  
  // 同步配置
  sync_type: SyncType;
  incremental_field?: string;
  incremental_field_type?: IncrementalFieldType;
  incremental_start_value?: string;
  last_incremental_value?: string;
  batch_size: number;
  
  // 调度配置
  schedule_type: ScheduleType;
  cron_expression?: string;
  
  // 状态信息
  is_active: boolean;
  last_execution_id?: number;
  last_execution_time?: string;
  last_execution_status?: ExecutionStatus;
  next_execution_time?: string;
  
  // 逻辑删除字段
  is_deleted: boolean;
  deleted_at?: string;
  deleted_by?: number;
  
  // 审计字段
  created_by?: number;
  updated_by?: number;
  created_at: string;
  updated_at: string;
  
  // 关联数据
  field_mappings: SyncFieldMapping[];
}

// 创建同步任务接口
export interface SyncTaskCreate {
  name: string;
  display_name: string;
  description?: string;
  source_datasource_id: number;
  source_table_name: string;
  source_query?: string;
  target_type: TargetType;
  target_datasource_id?: number;
  target_table_name?: string;
  target_path?: string;

  // CSV配置
  csv_delimiter?: string;
  csv_quote_char?: string;
  csv_escape_char?: string;
  csv_line_terminator?: string;
  csv_encoding?: string;
  csv_include_header?: boolean;
  csv_null_value?: string;
  csv_date_format?: string;
  sync_type: SyncType;
  incremental_field?: string;
  incremental_field_type?: IncrementalFieldType;
  incremental_start_value?: string;
  batch_size: number;
  schedule_type: ScheduleType;
  cron_expression?: string;
  is_active: boolean;
  field_mappings: SyncFieldMapping[];
}

// 更新同步任务接口
export interface SyncTaskUpdate {
  display_name?: string;
  description?: string;
  source_table_name?: string;
  source_query?: string;
  target_table_name?: string;
  target_path?: string;
  sync_type?: SyncType;
  incremental_field?: string;
  incremental_field_type?: IncrementalFieldType;
  batch_size?: number;
  schedule_type?: ScheduleType;
  cron_expression?: string;
  is_active?: boolean;
  field_mappings?: SyncFieldMapping[];
}

// 同步执行记录接口
export interface SyncExecution {
  id: number;
  task_id: number;
  execution_type: ExecutionType;
  status: ExecutionStatus;
  progress_percentage: number;
  
  // 执行统计
  total_records: number;
  processed_records: number;
  success_records: number;
  failed_records: number;
  
  // 时间信息
  started_at?: string;
  completed_at?: string;
  execution_time_ms?: number;
  
  // 错误信息
  error_message?: string;
  error_details?: any;
  
  // 执行参数
  execution_params?: any;
  
  // 断点续传信息
  checkpoint_data?: any;
  last_processed_value?: string;
  
  // 审计字段
  created_by?: number;
  created_at: string;
  updated_at: string;
}

// 同步日志接口
export interface SyncLog {
  id: number;
  execution_id: number;
  log_level: LogLevel;
  log_message: string;
  log_details?: any;
  batch_number?: number;
  created_at: string;
}



// CSV执行参数接口
export interface CSVExecutionParams {
  delimiter?: string;
  quote_char?: string;
  escape_char?: string;
  line_terminator?: string;
  encoding?: string;
  include_header?: boolean;
  null_value?: string;
  date_format?: string;
  escape_special_chars?: boolean;
}

// 执行请求接口
export interface SyncExecutionRequest {
  task_id: number;
  execution_params?: any;
  csv_params?: CSVExecutionParams;
  force_full_sync: boolean;
  custom_incremental_value?: string;
}

// 批量操作请求接口
export interface BatchSyncTaskRequest {
  task_ids: number[];
  action: 'enable' | 'disable' | 'delete' | 'execute';
}

// 批量操作响应接口
export interface BatchSyncTaskResponse {
  success_count: number;
  failed_count: number;
  results: any[];
}

// 列表响应接口
export interface SyncTaskListResponse {
  items: SyncTask[];
  total: number;
  page: number;
  page_size: number;
  pages: number;
}

export interface SyncExecutionListResponse {
  items: SyncExecution[];
  total: number;
  page: number;
  page_size: number;
  pages: number;
}

export interface SyncLogListResponse {
  items: SyncLog[];
  total: number;
  page: number;
  page_size: number;
  pages: number;
}

// 查询参数接口
export interface SyncTaskQueryParams {
  page?: number;
  page_size?: number;
  search?: string;
  sync_type?: SyncType;
  target_type?: TargetType;
  is_active?: boolean;
}

export interface SyncExecutionQueryParams {
  task_id?: number;
  status?: ExecutionStatus;
  page?: number;
  page_size?: number;
}

export interface SyncLogQueryParams {
  execution_id: number;
  page?: number;
  page_size?: number;
}
